﻿Imports System.Windows.Forms

Public Class frmEditCalendars

    Private lift As Integer = 0

    Private listColorPic As New List(Of Color)

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Dim frm1 As frmEditCalendar
        Dim i As Integer = 0
        frm1 = Me.Owner
        If frm1.CalendarCategory.Items.Count <> 0 Then
            frm1.CalendarCategory.Items.Clear()
            frm1.CalendarCategory.ColorList.Clear()
            frm1.CalendarCategory.Items.Add(New ComboBoxIconItem("       < Unfiled >", i))
            'Dim c As New Color()
            'c = Color.White
            frm1.CalendarCategory.ColorList.Add(Color.White)
            i += 1
            frm1.CalendarCategory.SelectedIndex = 0
        End If

        For Each ele As String In Me.ListCategories.Items
            frm1.CalendarCategory.Items.Add(New ComboBoxIconItem("       " & Trim(ele), i))
            i += 1
        Next
        For Each element As PictureBox In Me.ListCategories.Controls
            frm1.CalendarCategory.ColorList.Add(element.BackColor)
        Next
        frm1.CalendarCategory.Update()
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim frm1 As New frmNewCategory
        frm1.ShowDialog()
        If (frm1.DialogResult) Then
            Dim myPic As New PictureBox
            myPic.BackColor = listColorPic.Item(lift)
            myPic.Location = New Point(2, 16 * lift)
            myPic.Size = New Size(15, 15)
            lift += 1
            Me.ListCategories.Controls.Add(myPic)
            Me.ListCategories.Items.Add("       " & frm1.txtNameCategory.Text)
            'Me.ListCategories.Items.Add(myPic)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim id As Integer = Me.ListCategories.SelectedIndex
            Dim frm1 As New frmRenameCategory
            frm1.TextName.Text = Trim(Me.ListCategories.Items.Item(id).ToString)
            frm1.ShowDialog()
            If (frm1.DialogResult) Then
                Me.ListCategories.Items.Item(id) = "       " & Trim(frm1.TextName.Text)
            End If
        Else
            MessageBox.Show("You should choose one category or create a new one.")
        End If

        
    End Sub

    Private Sub picBox_Handle(ByVal e As ListBox)
        Dim ele As PictureBox
        For i As Integer = 1 To e.Controls.Count - 1
            ele = e.Controls.Item(i)
            ele.Location = New Point(2, 16 * i)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim id As Integer = Me.ListCategories.SelectedIndex
            Dim str As String = Me.ListCategories.Items.Item(id).ToString
            Me.ListCategories.Items.RemoveAt(id)
            Me.ListCategories.Controls.RemoveAt(id)
            lift -= 1
            picBox_Handle(Me.ListCategories)
            MessageBox.Show("The Category named " & Trim(str) & " is deleted, all fields are moved to unfiled!")
        Else
            MessageBox.Show("You should choose one category to delete.")
        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox1.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox2.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox3.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox4.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox5.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox13.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox13.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox6.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox14.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox14.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox7.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox15.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox15.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox8.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox16.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox16.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox9.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox17.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox17.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox10.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox10.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox18.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox18.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox11.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox11.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox19.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox19.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox12.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox12.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub PictureBox20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox20.Click
        If (Me.ListCategories.SelectedItem <> "") Then
            Dim myPic As PictureBox
            Dim id As Integer = Me.ListCategories.SelectedIndex
            myPic = Me.ListCategories.Controls.Item(id)
            myPic.BackColor = PictureBox20.BackColor
            myPic.Update()
        End If
    End Sub

    Private Sub frmEditCalendars_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listColorPic.Clear()
        listColorPic.Add(PictureBox1.BackColor)
        listColorPic.Add(PictureBox2.BackColor)
        listColorPic.Add(PictureBox3.BackColor)
        listColorPic.Add(PictureBox4.BackColor)
        listColorPic.Add(PictureBox5.BackColor)
        listColorPic.Add(PictureBox6.BackColor)
        listColorPic.Add(PictureBox7.BackColor)
        listColorPic.Add(PictureBox8.BackColor)
        listColorPic.Add(PictureBox9.BackColor)
        listColorPic.Add(PictureBox10.BackColor)
        listColorPic.Add(PictureBox11.BackColor)
        listColorPic.Add(PictureBox12.BackColor)
        listColorPic.Add(PictureBox13.BackColor)
        listColorPic.Add(PictureBox14.BackColor)
        listColorPic.Add(PictureBox15.BackColor)
        listColorPic.Add(PictureBox16.BackColor)
        listColorPic.Add(PictureBox17.BackColor)
        listColorPic.Add(PictureBox18.BackColor)
        listColorPic.Add(PictureBox19.BackColor)
        listColorPic.Add(PictureBox20.BackColor)

        Dim frm1 As frmEditCalendar
        frm1 = Me.Owner

        Dim str As String = ""
        'MessageBox.Show(frm1.CalendarCategory.Items.Count & " - " & frm1.CalendarCategory.ColorList.Count)

        If frm1.CalendarCategory.Items.Count > 1 Then
            For i As Integer = 1 To frm1.CalendarCategory.Items.Count - 1
                Dim myPic As New PictureBox
                myPic.BackColor = frm1.CalendarCategory.ColorList.Item(i)
                myPic.Location = New Point(2, 16 * lift)
                myPic.Size = New Size(15, 15)
                lift += 1
                Me.ListCategories.Controls.Add(myPic)
                Me.ListCategories.Items.Add("       " & Trim(frm1.CalendarCategory.Items.Item(i).ToString))
            Next
            Me.ListCategories.Update()
        End If
    End Sub
End Class
